package com.example;

/**
 * @Author loubobooo
 * @Description #198. 打家劫舍
 * @Date 2022/3/30
 */
public class HouseRobber {

    public int rob(int[] nums) {
        int length = nums.length;
        int[] dp = new int[length];
        // 初始0值
        dp[0] = nums[0];
        if(length == 1){
            return dp[length -1];
        }else if(length == 2){
            return Math.max(nums[0], nums[1]);
        }else{
            dp[1] = Math.max(nums[0], nums[1]);

            for (int i = 2; i < length; i++) {
                dp[i] = Math.max(dp[i - 1], dp[i -2] + nums[i]);
            }
            return dp[length -1];
        }
    }

    public static void main(String[] args) {
        HouseRobber houseRobber = new HouseRobber();
        houseRobber.rob(new int[]{1,2,3,1});
    }
}
